package com.ruoyi.mymod.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.mymod.service.IPatrolService;
import com.ruoyi.mymod.service.IPecProgramService;
import com.ruoyi.mymod.utils.Dbutil;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class PatrolServiceImpl implements IPatrolService {

    private static class TempObject {
        private String id;
        private String name;
        private String longtude;
        private String lattude;
        private String routecode;
        private List<child> children;


        public String getRoutecode() {
            return routecode;
        }

        public void setRoutecode(String routecode) {
            this.routecode = routecode;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getLongtude() {
            return longtude;
        }

        public void setLongtude(String longtude) {
            this.longtude = longtude;
        }

        public String getLattude() {
            return lattude;
        }

        public void setLattude(String lattude) {
            this.lattude = lattude;
        }

        public List<child> getChildren() {
            return children;
        }

        public void setChildren(List<child> children) {
            this.children = children;
        }
    }

    private static class child{
        private String date;
        private String route;
        private String sitename;
        private String pics;
        private String description;
        private String allDate;

        public String getAllDate() {
            return allDate;
        }

        public void setAllDate(String allDate) {
            this.allDate = allDate;
        }

        public String getDate() {
            return date;
        }

        public void setDate(String date) {
            this.date = date;
        }

        public String getRoute() {
            return route;
        }

        public void setRoute(String route) {
            this.route = route;
        }

        public String getSitename() {
            return sitename;
        }

        public void setSitename(String sitename) {
            this.sitename = sitename;
        }

        public String getPics() {
            return pics;
        }

        public void setPics(String pics) {
            this.pics = pics;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }
    }


    @Override
    public String getSitesList(String phone) {
        Dbutil.R r=null;
        Dbutil.Qp qp=null;
        String jsonstr="";
        try {
            qp = new Dbutil.Qp();
            Object o= qp.setUsrpwd("pigpos","pigpos").setSqlStr("SELECT f.*,m.routecode \n" +
                    "FROM WX_PATROL_USERROUTE m\n" +
                    "JOIN WX_PATROL_ROUTE t ON t.routecode = m.routecode\n" +
                    "JOIN WX_PATROL_SITE f ON f.sitecode = t.site\n" +
                    "WHERE m.usercode = '"+phone+"'\n" +
                    "ORDER BY t.sort ASC").select();
            r=(Dbutil.R) o;
            Object[][] body = r.body;
            List<String> sitenames = new ArrayList<>();
            for (Object[] subArray : body) {
                if (subArray != null && subArray.length >= 2) {
                    Object o1 = subArray[1];
                    sitenames.add(o1.toString());
                }
            }
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String nowdate = format.format(new Date());
            List<TempObject> tempObjects=new ArrayList<>();
            for (int i = 0; i <sitenames.size() ; i++) {
                TempObject tempObject = new TempObject();
                tempObject.setId(body[i][0]==null?"":body[i][0].toString());
                tempObject.setName(body[i][1]==null?"":body[i][1].toString());
                tempObject.setRoutecode(body[i][4]==null?"":body[i][4].toString());
                Object[][] patrolBySite = getPatrolBySite(phone, sitenames.get(i), nowdate);
                List<child> childList=new ArrayList<>();
                if (patrolBySite!=null){
                    for (int j = 0; j < patrolBySite.length; j++) {
                        child child = new child();
                        child.setDate(patrolBySite[j][0]==null?"":patrolBySite[j][0].toString());
                        child.setRoute(patrolBySite[j][1]==null?"":patrolBySite[j][1].toString());
                        child.setSitename(patrolBySite[j][2]==null?"":patrolBySite[j][2].toString());
                        child.setPics(patrolBySite[j][3]==null?"":patrolBySite[j][3].toString());
                        child.setDescription(patrolBySite[j][4]==null?"":patrolBySite[j][4].toString());
                        child.setAllDate(patrolBySite[j][0]==null?"":patrolBySite[j][0].toString().substring(11).substring(0, 5));
                    childList.add(child);
                    }
                }
                tempObject.setChildren(childList);
                tempObjects.add(tempObject);
            }
            ObjectMapper mapper = new ObjectMapper();
            jsonstr = mapper.writeValueAsString(tempObjects);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } finally {
            qp.closeConn();
        }
        return jsonstr;
    }

    @Override
    public String doPatrol(String phone, String nowdate, String route, String site, String pics, String description) {
        Dbutil.R r=null;
        Dbutil.Qp qp=null;
        try {
            qp = new Dbutil.Qp();
            Object o= qp.setUsrpwd("pigpos","pigpos").setSqlStr("insert into WX_PATROL_PATROL (usercode,PATROLDATE,Route,site,Pics,Description) values ('"+phone+"',to_date('"+nowdate+"','YYYY-MM-DD HH24:MI:SS'),'"+route+"','"+site+"','"+pics+"','"+description+"')").select();
            r=(Dbutil.R) o;

        }catch (Exception ex){
            return "fail";
        }finally {
            qp.closeConn();
        }
        return "success";
    }

    @Override
    public String getUsers() {
        Dbutil.R r=null;
        Dbutil.Qp qp=null;
        try {
            qp = new Dbutil.Qp();
            Object o= qp.setUsrpwd("pigpos","pigpos").setSqlStr("select t.phone,t.user_name from WX_USER t where t.patrolpermission!='check'").select();
            r=(Dbutil.R) o;
        }finally {
            qp.closeConn();
        }
        return r.json;
    }

    @Override
    public String getRecord(String phone, String date) {
        Dbutil.R r=null;
        Dbutil.Qp qp=null;
        try {
            String datestr="";
            if ("".equals(date) || date==null){
                datestr="1=1";
            }else {
                datestr="to_char(PATROLDATE,'yyyy-mm-dd')='"+date+"'";
            }
            qp = new Dbutil.Qp();
            Object o= qp.setUsrpwd("pigpos","pigpos").setSqlStr("select to_char(t.PATROLDATE,'YYYY-MM-DD HH24:MI:SS'),t.route,m.sitename,t.pics,t.description from WX_PATROL_PATROL t,WX_PATROL_SITE m where t.site=m.sitecode and usercode='"+phone+"' and "+datestr+" order by t.route,TO_NUMBER(t.site),t.PATROLDATE desc").select();
            r=(Dbutil.R) o;
        }finally {
            qp.closeConn();
        }
        return r.json;
    }


    @Override
    public Object[][] getPatrolBySite(String phone, String site,String date) {
        Dbutil.R r=null;
        Dbutil.Qp qp=null;
        Object[][] obj=null;
        try {

            qp = new Dbutil.Qp();
            Object o= qp.setUsrpwd("pigpos","pigpos").setSqlStr("select to_char(t.PATROLDATE,'YYYY-MM-DD HH24:MI:SS'),t.route,m.sitename,t.pics,t.description from WX_PATROL_PATROL t,WX_PATROL_SITE m where t.site=m.sitecode and m.sitename='"+site+"' and usercode='"+phone+"' and to_char(t.PATROLDATE,'yyyy-mm-dd')='"+date+"' order by t.route,TO_NUMBER(t.site),t.PATROLDATE desc").select();
            r=(Dbutil.R) o;
            if (r.body!=null && r.body.length>0){
                obj=r.body;
            }
        }finally {
            qp.closeConn();
        }
        return obj;
    }

}
